package com.work.csms.mapper;

import com.work.csms.dao.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMapper {

    @Select({"<script>" +
            "SELECT * FROM `order` " +
            " where 1 = 1" +
            "<if test=\"id != null and id != '' \"> " +
            " AND id like concat('%',#{id},'%')" +
            "</if>" +
            " limit #{offset} , #{size}" +
            "</script>"})
    List<Order> page(Integer id, Integer offset, Integer size);

    @Select({"<script>" +
            "SELECT count(0) FROM `order` " +
            "where 1 = 1" +
            "<if test=\"id != null and id != '' \"> " +
            " AND id like concat('%',#{id},'%')" +
            "</if>" +
            "</script>"})
    Integer count(Integer id);

    @Select({"<script>" +
            "SELECT o.*,s.name as stationname FROM `order` o left join station s on o.stationid = s.id " +
            "where 1 = 1" +
            "<if test=\"userid != null\"> " +
            " AND o.userid = #{userid}" +
            "</if>" +
            "<if test=\"statu != null and statu != '' \"> " +
            " AND o.statu = #{statu}" +
            "</if>" +
            " order by start desc " +
            "</script>"})
    List<Order> list(Order order);

    @Select({"<script>" +
            "SELECT -sum(money) FROM `order` " +
            " where start  &gt;= #{start} and start &lt;= #{end}" +
            "</script>"})
    Integer getOrderMoney(Order order);

    @Select("SELECT * FROM `order` WHERE id=#{id}")
    Order getOrderById(Integer id);


    @Update({"<script>" +
            "update `order` set " +
            "<if test=\"userid != null \"> " +
            " userid = #{userid}," +
            "</if>" +
            "<if test=\"stationid != null \"> " +
            " stationid = #{stationid}," +
            "</if>" +
            "<if test=\"money != null \"> " +
            " money = #{money}," +
            "</if>" +
            "<if test=\"statu != null and statu != '' \"> " +
            " statu = #{statu}," +
            "</if>" +
            "<if test=\"start != null \"> " +
            " start = #{start}," +
            "</if>" +
            "<if test=\"end != null \"> " +
            " end = #{end}," +
            "</if>" +
            " id = #{id}" +
            " where id = #{id}" +
            "</script>"})
    void update(Order order);

    @Insert({"<script>" +
            "INSERT INTO `order` (userid, stationid, money, statu, start) VALUES " +
            "(#{userid}, #{stationid}, #{money}, #{statu}, #{start})" +
            "</script>"})
    void save(Order order);

    @Delete("delete from `order` where id = #{id} ")
    void deleted(Integer id);
}
